package com.witframework.web.extremetable;


import org.apache.commons.lang.StringUtils;
import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.cell.Cell;
import org.extremecomponents.table.core.MessagesConstants;
import org.extremecomponents.table.core.PreferencesConstants;
import org.extremecomponents.table.core.TableConstants;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.core.TableModelUtils;
import org.extremecomponents.table.view.html.BuilderConstants;
import org.extremecomponents.table.view.html.BuilderUtils;
import org.extremecomponents.util.HtmlBuilder;



/**
 * A header cell.
 *
 * @author Jeff Johnston
 */
public class WsHeaderCell implements Cell {
    public String getExportDisplay(TableModel model, Column column) {    	
        return column.getTitle();
    }

    public String getHtmlDisplay(TableModel model, Column column) {
        HtmlBuilder html = new HtmlBuilder();

        String headerClass = null;
        String sortImage = null;
        String sortOrder = null;
        headerClass = column.getHeaderClass();       
        if (TableModelUtils.isSorted(model, column.getAlias())) {        	
            sortOrder = model.getLimit().getSort().getSortOrder();
            if (sortOrder.equals(TableConstants.SORT_DEFAULT)) {
                sortOrder = TableConstants.SORT_ASC;
            } else if (sortOrder.equals(TableConstants.SORT_ASC)) {
                headerClass = model.getPreferences().getPreference(PreferencesConstants.TABLE_HEADER_SORT_CLASS);                
                sortImage = BuilderUtils.getImage(model, BuilderConstants.SORT_ASC_IMAGE);
                sortOrder = TableConstants.SORT_DESC;
            } else if (sortOrder.equals(TableConstants.SORT_DESC)) {
                headerClass = model.getPreferences().getPreference(PreferencesConstants.TABLE_HEADER_SORT_CLASS);               
                sortImage = BuilderUtils.getImage(model, BuilderConstants.SORT_DESC_IMAGE);
                sortOrder = TableConstants.SORT_DEFAULT;
            }
        } else {
            sortOrder = TableConstants.SORT_ASC; // the default
        }        
        buildHeaderHtml(html, model, column, headerClass, sortImage, sortOrder);
        return html.toString();
    }

    protected void buildHeaderHtml(HtmlBuilder html, TableModel model, Column column, String headerClass, String sortImage, String sortOrder) {
    	String hasComplexTitle=model.getTableHandler().getTable().getAttributeAsString("hasComplexTitle");
        if(hasComplexTitle!=null){
	    	HtmlBuilder title2=new HtmlBuilder();   
	    	if(!hasComplexTitle.equalsIgnoreCase("true")&&!hasComplexTitle.equalsIgnoreCase("Y"))
	        	title2.append(hasComplexTitle);
	    	String group=column.getAttributeAsString("group");
	    	if(group!=null){
	    		Object displayed= model.getTableHandler().getTable().getAttribute(group);
	    		if(displayed!=null)
	    			return;
	    		else{
	    			String title=column.getTitle();
	    			String[] titleRows=StringUtils.split(title, "|");
	    			if(titleRows.length==2){
	    				String[] titleRow2=StringUtils.split(titleRows[1], ",");	    				
	    				html.td(2).colSpan(String.valueOf(titleRow2.length));
	    				html.align("center");
	    				addStyle(html,model,column,headerClass,sortImage,sortOrder);    				
	    		        html.close();
	    		        html.append(titleRows[0]);
	    		        html.tdEnd();	    		        
	    		   
	    		        for(String row2Title:titleRow2){
	    		        	if(title2.length()==0){	 
	    		        		title2.tr(1);
	    		        		title2.close();
	    		        	}	    		        	
	    		        	title2.td(2).align("center");	    		        	
	    		        	addStyle(title2,model,column,headerClass,sortImage,sortOrder);
	    		        	title2.close();	    		        	
	    		        	title2.append(row2Title);
	        		        addSortImg(title2,model,column,headerClass,sortImage,sortOrder);	 
	        		        title2.tdEnd();
	    		        }
	    		        model.getTableHandler().getTable().addAttribute("hasComplexTitle", title2.toString());
	    		        
	    			}	  			
	    			
	    			
	    		}
	    		model.getTableHandler().getTable().addAttribute(group,"display");
	    	}else{	    		
		    	html.td(2);
		    	html.rowSpan("2");
		        String property= column.getProperty();
		        if(property.equals("checkbox")){//如果列的property值设置为checkbox则在表头栏显示全选的checkbox
		        	String checkboxName="itemlist";
		        	String title=column.getTitle();
		        	if(title.startsWith("\\S")){
		        		checkboxName=title;
		        	}
		        	column.setSortable(false);
		        	column.setTitle("<input type=checkbox onclick=setEcCheckState('"+checkboxName+"',this.checked)>");
		        }		
		        addStyle(html,model,column,headerClass,sortImage,sortOrder);
		        addSortScript(html,model,column,headerClass,sortImage,sortOrder);
		        html.close();		
		        html.append(column.getTitle());
		
		        addSortImg(html,model,column,headerClass,sortImage,sortOrder);	        
		
		        html.tdEnd();
	    	}
    	}else{   	
    		
	    	html.td(2);
	        String property= column.getProperty();
	        if(property.equals("checkbox")){//如果列的property值设置为checkbox则在表头栏显示全选的checkbox
	        	String checkboxName="itemlist";
	        	String title=column.getTitle();
	        	if(title.startsWith("\\S")){
	        		checkboxName=title;
	        	}
	        	column.setSortable(false);
	        	column.setTitle("<input type=checkbox onclick=setEcCheckState('"+checkboxName+"',this.checked)>");
	        }	        
	
	        addStyle(html,model,column,headerClass,sortImage,sortOrder);
	        addSortScript(html,model,column,headerClass,sortImage,sortOrder);
	        html.close();
	
	        html.append(column.getTitle());
	
	        addSortImg(html,model,column,headerClass,sortImage,sortOrder);	        
	
	        html.tdEnd();
    	}
	    
    }
    private void addStyle(HtmlBuilder html, TableModel model, Column column, String headerClass, String sortImage, String sortOrder){
    	 	if (StringUtils.isNotEmpty(headerClass)) {
	            html.styleClass(headerClass);
	        }	
	        if (StringUtils.isNotEmpty(column.getHeaderStyle())) {
	            html.style(column.getHeaderStyle());
	        }
	
	        if (StringUtils.isNotEmpty(column.getWidth())) {
	            html.width(column.getWidth());
	        }	
	        
    }
    private void addSortScript(HtmlBuilder html, TableModel model, Column column, String headerClass, String sortImage, String sortOrder){
    	if (column.isSortable()) {
            if (sortOrder.equals(TableConstants.SORT_ASC)) {
                html.onmouseover("this.className='" + BuilderConstants.TABLE_HEADER_SORT_CSS + "';this.style.cursor='pointer'");
                if (StringUtils.isNotEmpty(headerClass)) {
                    html.onmouseout("this.className='" + headerClass + "';this.style.cursor='default'");
                } else {
                    html.onmouseout("this.className='" + BuilderConstants.TABLE_HEADER_CSS + "';this.style.cursor='default'");
                }
            }
            if (sortOrder.equals(TableConstants.SORT_DEFAULT) || sortOrder.equals(TableConstants.SORT_DESC)) {
                html.onmouseover("this.style.cursor='pointer'");
                html.onmouseout("this.style.cursor='default'");
            }
            html.onclick(new WsTableActions(model).getSortAction(column, sortOrder));

            boolean showTooltips = model.getTableHandler().getTable().isShowTooltips();
            if (showTooltips) {
                String headercellTooltip = model.getMessages().getMessage(MessagesConstants.COLUMN_HEADERCELL_TOOLTIP_SORT);
                html.title(headercellTooltip + " " + column.getTitle());
            }
        }	
    }
    private void addSortScriptByColumnAlias(HtmlBuilder html, TableModel model, Column column, String headerClass,String sortImage, String sortOrder,String columnAlias){
    	if (column.isSortable()) {
            if (sortOrder.equals(TableConstants.SORT_ASC)) {
                html.onmouseover("this.className='" + BuilderConstants.TABLE_HEADER_SORT_CSS + "';this.style.cursor='pointer'");
                if (StringUtils.isNotEmpty(headerClass)) {
                    html.onmouseout("this.className='" + headerClass + "';this.style.cursor='default'");
                } else {
                    html.onmouseout("this.className='" + BuilderConstants.TABLE_HEADER_CSS + "';this.style.cursor='default'");
                }
            }
            if (sortOrder.equals(TableConstants.SORT_DEFAULT) || sortOrder.equals(TableConstants.SORT_DESC)) {
                html.onmouseover("this.style.cursor='pointer'");
                html.onmouseout("this.style.cursor='default'");
            }
            html.onclick(new WsTableActions(model).getSortAction(columnAlias, sortOrder));

            boolean showTooltips = model.getTableHandler().getTable().isShowTooltips();
            if (showTooltips) {
                String headercellTooltip = model.getMessages().getMessage(MessagesConstants.COLUMN_HEADERCELL_TOOLTIP_SORT);
                html.title(headercellTooltip + " " + column.getTitle());
            }
        }	
    }
    private void addSortImg(HtmlBuilder html, TableModel model, Column column, String headerClass, String sortImage, String sortOrder){
    	if (column.isSortable()) {
            if (StringUtils.isNotEmpty(sortImage)) {
                html.nbsp();
                if(sortImage.indexOf("Asc")>-1)
                	html.append("↑");
                else if(sortImage.indexOf("Desc")>-1)
                	html.append("↓");
//                html.img();
//                html.src(sortImage);
//                html.style("border:0");
//                html.alt("Arrow");
//                html.xclose();
            }
        }
    }
}

